package com.yjhb.android.sbz.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yjhb.android.sbz.R;
import com.jojo.android.zxlib.tool.ZxTool;

/**
 * 创建者 zx
 * 创建时间 2020/11/23 9:22
 */
public class VerticalView extends LinearLayout {
    private final String TAG = "VerticalView";
    private ImageView imageView;
    private TextView titleView;

    public VerticalView(Context context) {
        this(context, null);
    }

    public VerticalView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VerticalView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.VerticalView);
        imageView.setImageDrawable(array.getDrawable(R.styleable.VerticalView_ver_img));
        int w = array.getDimensionPixelSize(R.styleable.VerticalView_ver_img_width, -2);
        int h = array.getDimensionPixelSize(R.styleable.VerticalView_ver_img_height, -2);
        int spacing = array.getDimensionPixelSize(R.styleable.VerticalView_ver_spacing, ZxTool.dp2px(5));
        LinearLayout.LayoutParams params = null;
        params = new LayoutParams(getContext(), attrs);
        imageView.setLayoutParams(params);
        params.bottomMargin = spacing;
        params.width = w;
        params.height = h;
        titleView.setText(array.getText(R.styleable.VerticalView_ver_text));
        titleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, array.getDimensionPixelSize(R.styleable.VerticalView_ver_textSize,
                ZxTool.sp2px(12)));
        titleView.setTextColor(array.getColor(R.styleable.VerticalView_ver_textColor, Color.BLACK));
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_vertical_view, this, true);
        imageView = findViewById(R.id.image_iv);
        titleView = findViewById(R.id.title_tv);
    }


    public void setTitleColor(@ColorInt int color) {
        titleView.setTextColor(color);
    }

    public void setTitleText(CharSequence text) {
        titleView.setText(text);
    }

    public void setTitleSize(int size) {
        titleView.setTextSize(size);
    }

    public ImageView getImageView() {
        return imageView;
    }

    public TextView getTitleView() {
        return titleView;
    }
}
